//
//  PCDApiUploadFiles.m
//  PCDBankSix
//
//  Created by Jason Tao on 2022/4/27.
//

#import "PCDApiUploadFiles.h"

@implementation PCDApiUploadFiles

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback
{
    NSArray * arrFiles = [data objectForKey:@"fileList"];
    NSString * strUrl = [data objectForKey:@"url"];
    NSString * strZipName = [data objectForKey:@"zipFileName"];
    NSDictionary * dicParam = [data objectForKey:@"param"];
    NSString * strSuccessFunc = data[@"success"];
    NSString * strFailFunc = data[@"fail"];
    NSMutableArray * arrLocalFilePath = [[NSMutableArray alloc] initWithCapacity:0];
    
    NSString *locationPath = PackageSericePath;

    if (arrFiles.count>0) {
        for (NSString * filePath in arrFiles) {
            NSString * localFilePath = [filePath stringByReplacingOccurrencesOfString:[[PCDPackageUitil sharedService] getSericeUrl] withString:@""];
            localFilePath = [NSString stringWithFormat:@"%@%@",locationPath,localFilePath];
            [arrLocalFilePath addObject:localFilePath];
        }
    }
    //图片压缩
    NSString * zipFile = [PCDFile tempWithPath:strZipName];
    if ([PCDFile fileExistsAtPath:zipFile]) {
        [PCDFile removeItemAtPath:zipFile];
    }
    [PCMP_SSZipArchive createZipFileAtPath:zipFile withFilesAtPaths:arrLocalFilePath];
    NSData *fileData = [NSData dataWithContentsOfFile:zipFile];
    UIViewController * currentVC = context.viewController;
	[PCDWaittingService showWaitWithTitle:nil isFullScreen:YES parent:currentVC.view];
    __weak typeof(self) weakSelf = self;
    [PCDNetWorkServiceGet() sendRequestWithfile:[PCDUtil getServerURL:strUrl] body:dicParam File:fileData FileType:@"zip" FileName:strZipName andCompletionBlock:^(PCDNetWorkItem * _Nonnull item, BOOL success) {
		[PCDWaittingService hiddenForParent:currentVC.view];
        if (success) {
            evaluateJavaScriptOnMainThread(weakSelf,strSuccessFunc,item.responseString);
        }else{
            evaluateJavaScriptOnMainThread(weakSelf,strFailFunc,item.responseString);
        }
    }];
    
}

@end
